﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Data;
using System.Drawing;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AyaWo.WinForm
{
    public partial class ServForm : Form
    {
        public ServForm()
        {
            InitializeComponent();
            lab_serveName.Text= "WinService";
            this.skinEngine1.SkinFile = "DeepCyan.ssk";
        }

        string serviceFilePath = $"{Application.StartupPath}\\WinService.exe";
        string serviceName = "WinService";


        /// <summary>
        /// 事件：安装服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_InsertServe_Click(object sender, EventArgs e)
        {
            if (ServicesMothe.IsServiceExisted(serviceName))
                ServicesMothe.UninstallService(serviceName);
            ServicesMothe.InstallService(serviceFilePath);
            MessageBox.Show("服务安装成功...");
        }
        /// <summary>
        /// 事件：启动服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_StarServe_Click(object sender, EventArgs e)
        {
            if (ServicesMothe.IsServiceExisted(serviceName))
            {
                ServicesMothe.ServiceStart(serviceName);
                MessageBox.Show("启动服务成功...");
            }
        }
        /// <summary>
        /// 事件：停止服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_StopServe_Click(object sender, EventArgs e)
        {
            if (ServicesMothe.IsServiceExisted(serviceName))
            {
                ServicesMothe.ServiceStop(serviceName);
                MessageBox.Show("停止服务成功...");
            }
        }

        /// <summary>
        /// 事件：卸载服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_UntilServe_Click(object sender, EventArgs e)
        {
            if (ServicesMothe.IsServiceExisted(serviceName))
            {
                ServicesMothe.ServiceStop(serviceName);
                ServicesMothe.UninstallService(serviceFilePath);
                MessageBox.Show("卸载服务成功...");
            }
        }
        /// <summary>
        /// 判断服务是否存在
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_StartServe_Click(object sender, EventArgs e)
        {
            try
            {
                ServiceController serviceController = new ServiceController(serviceName);
                string Status = serviceController.Status.ToString();
                lab_ServeInfo.Text = "当前服务状态是：" + Status;
            }
            catch (Exception ex)
            {
                lab_ServeInfo.Text = "查看状态出现错误：" + ex.Message;
            }
        }

        private void btn_hz_Click(object sender, EventArgs e)
        {
            HZForm hz = new WinForm.HZForm();
            hz.ShowDialog();
        }

        private void btn_spler_Click(object sender, EventArgs e)
        {
            SplerForm sp = new WinForm.SplerForm();
            sp.ShowDialog();
        }

        private void btn_so_Click(object sender, EventArgs e)
        {
            SoForm so = new WinForm.SoForm();
            so.ShowDialog();
        }

        private void btn_Tonbu_Click(object sender, EventArgs e)
        {
            TonbuForm tb = new WinForm.TonbuForm();
            tb.ShowDialog();
        }


    }

    public class ServicesMothe
    {
        //判断服务是否存在
        public static bool IsServiceExisted(string serviceName)
        {
            ServiceController[] services = ServiceController.GetServices();
            foreach (ServiceController sc in services)
            {
                if (sc.ServiceName.ToLower() == serviceName.ToLower())
                {
                    return true;
                }
            }
            return false;
        }

        //安装服务
        public static void InstallService(string serviceFilePath)
        {
            using (AssemblyInstaller installer = new AssemblyInstaller())
            {
                installer.UseNewContext = true;
                installer.Path = serviceFilePath;
                IDictionary savedState = new Hashtable();
                installer.Install(savedState);
                installer.Commit(savedState);
            }

        }

        //卸载服务
        public static void UninstallService(string serviceFilePath)
        {
            using (AssemblyInstaller installer = new AssemblyInstaller())
            {
                installer.UseNewContext = true;
                installer.Path = serviceFilePath;
                installer.Uninstall(null);
            }

        }
        //启动服务
        public static void ServiceStart(string serviceName)
        {
            using (ServiceController control = new ServiceController(serviceName))
            {
                if (control.Status == ServiceControllerStatus.Stopped)
                {
                    control.Start();
                }
            }

        }

        //停止服务
        public static void ServiceStop(string serviceName)
        {
            using (ServiceController control = new ServiceController(serviceName))
            {
                if (control.Status == ServiceControllerStatus.Running)
                {
                    control.Stop();
                }
            }
        }

    }
}
